Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Backend.Models;
- using Backend.Models.User;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Web;
- using System.Web.Http;
- using System.Web.Http.Results;
- namespace Backend.Controllers
- {
- /// <summary>
- /// Controller for user
- /// </summary>
- [RoutePrefix("api/user")]
- public class UserController : ApiController
- {
- /// <summary>
- /// Return an array of Users
- /// </summary>
- /// <returns>Users</returns>
- [Route("getall")]
- [HttpPost]
- public UserModel[] GetUsers()
- {
- using (APIContext ctx = new APIContext())
- {
- UserModel[] users = ctx.Users.Where(x => x.accType == 0).ToArray();
- return users;
- }
- }
- /// <summary>
- /// Return an array of Entrprises
- /// </summary>
- /// <returns>Users</returns>
- [Route("getallenterprise")]
- [HttpPost]
- public UserModel[] GetEnterprises([FromBody] CoordinateSearch model)
- {
- if (model == null)
- {
- model = new CoordinateSearch();
- Validate(model);
- }
- if (!ModelState.IsValid)
- {
- return null;
- }
- using (APIContext ctx = new APIContext())
- {
- UserModel[] users = ctx.Users.Where(x => x.accType == 1).OrderBy(x => Math.Abs(x.xCord - model.xCord)).OrderBy(x => Math.Abs(x.yCord - model.yCord)).ToArray();
- return users;
- }
- }
- /// <summary>
- /// Returns a specific user at Id
- /// </summary>
- /// <param name="id"></param>
- /// <returns>User</returns>
- [Route("getuser")]
- [HttpPost]
- public UserModel GetUser([FromBody]FindUser model)
- {
- if (model == null)
- {
- model = new FindUser();
- Validate(model);
- }
- if (!ModelState.IsValid)
- {
- return null;
- }
- using (APIContext ctx = new APIContext())
- {
- if (String.IsNullOrWhiteSpace(model.name) == false)
- return ctx.Users.FirstOrDefault(x => x.name == model.name);
- if (model.id != 0)
- return ctx.Users.FirstOrDefault(x => x.id == model.id);
- }
- return null;
- }
- /// <summary>
- /// Update a user at a specific Id with new information.
- /// </summary>
- /// <param name="model">Model to use to update user</param>
- [Route("update")]
- [HttpPost]
- public HttpResponseMessage Update([FromBody] UpdateUserModel model)
- {
- if (model == null)
- {
- model = new UpdateUserModel();
- Validate(model);
- }
- if (!ModelState.IsValid)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest);
- }
- string newProfilePic = "";
- if (model.pic != null)
- newProfilePic = StorageManager.UploadToStorage(model.pic);
- using (APIContext ctx = new APIContext())
- {
- UserModel user = ctx.Users.FirstOrDefault(x => x.id == model.id);
- if(user == new UserModel() || user == null)
- {
- return Request.CreateResponse(HttpStatusCode.NotFound);
- }
- if(String.IsNullOrWhiteSpace(model.name) == false)
- user.name = model.name;
- if (String.IsNullOrWhiteSpace(newProfilePic) == false)
- user.profilePic = newProfilePic;
- user.dateUpdated = DateTime.UtcNow;
- ctx.SaveChanges();
- }
- return Request.CreateResponse(HttpStatusCode.OK);
- }
- /// <summary>
- /// Creates a user
- /// </summary>
- /// <param name="model">Data to enter</param>
- [Route("create")]
- [HttpPost]
- public HttpResponseMessage Create([FromBody] CreateUserModel model)
- {
- if (model == null)
- {
- model = new CreateUserModel();
- Validate(model);
- }
- if (!ModelState.IsValid)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest);
- }
- string newPic = "";
- if (model.pic != null)
- newPic = StorageManager.UploadToStorage(model.pic);
- using (APIContext ctx = new APIContext())
- {
- if (ctx.Users.FirstOrDefault(x => x.email == model.email) != null)
- return Request.CreateResponse(HttpStatusCode.Conflict);
- UserModel newModel = new UserModel();
- newModel.name = model.name;
- newModel.accType = model.accType;
- newModel.xCord = model.xCord;
- newModel.yCord = model.yCord;
- newModel.email = model.email;
- newModel.password = model.password;
- newModel.profilePic = newPic;
- newModel.score = 0;
- newModel.dateCreated = DateTime.UtcNow;
- newModel.dateUpdated = DateTime.UtcNow;
- ctx.Users.Add(newModel);
- ctx.SaveChanges();
- }
- return Request.CreateResponse(HttpStatusCode.OK);
- }
- /// <summary>
- /// Delete a user at an id
- /// </summary>
- /// <param name="model">Model to delete</param>
- // DELETE api/User/5
- [Route("deleteuser")]
- [HttpPost]
- public HttpResponseMessage DeleteUser([FromBody]FindUser model)
- {
- if (model == null)
- {
- model = new FindUser();
- Validate(model);
- }
- if (!ModelState.IsValid)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest);
- }
- using (APIContext ctx = new APIContext())
- {
- if (String.IsNullOrWhiteSpace(model.name) == false)
- ctx.Users.Remove(ctx.Users.FirstOrDefault(x => x.name == model.name));
- if (model.id != 0)
- ctx.Users.Remove(ctx.Users.FirstOrDefault(x => x.id == model.id));
- ctx.SaveChanges();
- }
- return Request.CreateResponse(HttpStatusCode.OK);
- }
- /// <summary>
- /// Log a user in using email and password
- /// </summary>
- /// <param name="authModel"></param>
- /// <returns>If Valid: id | If Invalid: Error</returns>
- // POST api/User/Login
- [Route("login")]
- [HttpPost]
- public HttpResponseMessage Login([FromBody] AuthModel model)
- {
- if (model == null)
- {
- model = new AuthModel();
- Validate(model);
- }
- if (!ModelState.IsValid)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest);
- }
- model.password = Encryption.ComputeSha256Hash(model.password);
- UserModel userModel = new UserModel();
- using (APIContext ctx = new APIContext())
- {
- userModel = ctx.Users.FirstOrDefault(x => x.email == model.email && x.password == model.password);
- }
- if (userModel != null)
- return Request.CreateResponse(HttpStatusCode.OK, userModel.id);
- else
- return Request.CreateResponse(HttpStatusCode.NotFound);
- }
- /// <summary>
- /// Register a new User
- /// </summary>
- /// <param name="model">New data</param>
- /// <returns>Result</returns>
- // POST api/User/Register
- [Route("register")]
- [HttpPost]
- public HttpResponseMessage Register([FromBody] CreateUserModel model)
- {
- if(model == null)
- {
- model = new CreateUserModel();
- Validate(model);
- }
- if(!ModelState.IsValid)
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest);
- }
- model.password = Encryption.ComputeSha256Hash(model.password);
- UserModel newModel = new UserModel();
- newModel.name = model.name;
- newModel.accType = model.accType;
- newModel.xCord = model.xCord;
- newModel.yCord = model.yCord;
- newModel.email = model.email;
- newModel.password = model.password;
- newModel.score = 0;
- newModel.dateCreated = DateTime.UtcNow;
- newModel.dateUpdated = DateTime.UtcNow;
- if (model.pic != null)
- newModel.profilePic = StorageManager.UploadToStorage(model.pic);
- else
- newModel.profilePic = "";
- int newId = 0;
- using (APIContext ctx = new APIContext())
- {
- if (ctx.Users.FirstOrDefault(x => x.email == newModel.email) != null)
- return Request.CreateResponse(HttpStatusCode.Conflict);
- newModel = ctx.Users.Add(newModel);
- ctx.SaveChanges();
- newId = newModel.id;
- }
- return Request.CreateResponse(HttpStatusCode.OK, newId);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement