Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Rejestracja
- [HttpPost]
- [Route("addaccount")]
- public IHttpActionResult addAccount(AccountDTO account)
- {
- var acc = context.AccountSet.FirstOrDefault((a) => a.Login == account.Login);
- if(acc != null)
- {
- return BadRequest();
- }
- context.AccountSet.Add(new Account() {
- Login = account.Login,
- Password = account.Password
- });
- context.SaveChangesAsync();
- return Ok();
- }
- //Logowanie
- [HttpPost]
- [Route("login")]
- public HttpResponseMessage loginOnAccount(AccountDTO account)
- {
- var acc = context.AccountSet.FirstOrDefault((a) => a.Login == account.Login && a.Password == account.Password);
- if(acc != null)
- {
- return Request.CreateResponse(System.Net.HttpStatusCode.OK, acc.Id);
- }
- return Request.CreateResponse(System.Net.HttpStatusCode.BadRequest);
- }
- //Zwrocenie listy ogloszen uzytkownika
- [HttpGet]
- [Route("myadverts/{id}")]
- public IEnumerable<AdvertDTO> getMyAdverts(int id)
- {
- List<AdvertDTO> advertslist = new List<AdvertDTO>();
- foreach (var adv in context.AdvertSet.Where(b => b.AccountId == id))
- {
- advertslist.Add(Mapper.Map<AdvertDTO>(adv));
- }
- return advertslist.AsEnumerable();
- }
- //Dodawanie ogloszenia
- [HttpPost]
- [Route("addadvert/{id}")]
- public IHttpActionResult addAdvert(int id ,AdvertDTO advert)
- {
- var myNewAdvert = context.AdvertSet.FirstOrDefault((a) => a.Title == advert.Title);
- if (myNewAdvert != null)
- {
- return BadRequest();
- }
- Account acc = context.AccountSet.FirstOrDefault((a) => a.Id == id);
- if(acc == null)
- {
- return BadRequest();
- }
- myNewAdvert = Mapper.Map<Advert>(advert);
- myNewAdvert.Account = acc;
- myNewAdvert.AccountId = id;
- myNewAdvert.DateAdded = System.DateTime.Now;
- myNewAdvert.DateModified = System.DateTime.Now;
- context.AdvertSet.Add(myNewAdvert);
- context.SaveChangesAsync();
- return Ok();
- }
- //Aktualizacja ogloszenia
- [HttpPut]//TODO Zmiana elementow myAdvert z advert
- [Route("updateadvert/{userid}/{advertid}")]
- public IHttpActionResult updateAdvert(int userid, int advertid, AdvertDTO advert)
- {
- var myAdvert = context.AdvertSet.FirstOrDefault((a) => a.AccountId == userid && a.Id == advertid);
- if (myAdvert == null)
- return BadRequest();
- context.SaveChangesAsync();
- return Ok();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement